﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Kairos.Controllers
{
    public class CookieDocsController : Controller
    {
        //
        // GET: /Cookie/Create

        [AccessControl(Roles.All)]
        public ActionResult Create(string search, int? metaData_1ID, int? metaData_2ID)
        {
            search = search.Trim();

            HttpCookie cookie = new HttpCookie(Utilities.CookieNameDocsSearchstring);
            cookie.Value = new JavaScriptSerializer().Serialize(new object[] { search, metaData_1ID, metaData_2ID });
            Response.Cookies.Add(cookie);

            return RedirectToAction("Document", "Search");
        }

        //
        // GET: /Cookie/Remove

        /// <summary></summary>
        /// <returns></returns>
        /// <remarks>        
        /// You cannot directly delete a cookie on a user's computer. 
        /// However, you can direct the user's browser to delete the cookie 
        /// by setting the cookie's expiration date to a past date. 
        /// The next time a user makes a request to a page within the domain or path that set the cookie, 
        /// the browser will determine that the cookie has expired and remove it.
        /// Calling the Remove method of the Cookies collection 
        /// removes the cookie from the collection on the server side, 
        /// so the cookie will not be sent to the client. 
        /// However, the method does not remove the cookie from the client if it already exists there.
        /// </remarks>
        [AccessControl(Roles.All)]
        public ActionResult Remove()
        {
            if (Request.Cookies.AllKeys.Contains(Utilities.CookieNameDocsSearchstring))
            {
                HttpCookie cookie = Request.Cookies[Utilities.CookieNameDocsSearchstring];
                cookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(cookie);
            }
            return RedirectToAction("Document", "Search");
        }
    }
}
